package com.googlecode.flealancemarket.tapestry.pages;

import com.googlecode.flealancemarket.core.tapestry.SpringBean;
import com.googlecode.flealancemarket.metamodel.Category;
import com.googlecode.flealancemarket.metamodel.Job;
import com.googlecode.flealancemarket.service.commands.GetCategoryWithJobs;
import com.googlecode.flealancemarket.tapestry.util.Session;
import java.util.Calendar;
import java.util.List;
import java.util.Set;
import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.ioc.annotations.Inject;

public class Jobs {

    @ApplicationState
    private Session session;

    private int categoryId;
    private Job job;
    private List<Job> jobsList;

    public void setCategoryId(int id){
        categoryId = id;
    }
    public int getCategoryId(){
        return categoryId;
    }
    public List<Job> getJobsList() {
        return jobsList;
    }
    public void setJobsList(List<Job> list){
        this.jobsList = list;
    }
    public Job getJob() {
        return job;
    }
    public void setJob(Job job) {
        this.job = job;
    }

    public boolean getIsActive(){
        return Calendar.getInstance().getTimeInMillis() < job.getEndingTime().getTime();
    }

    @Inject
    @SpringBean("com.googlecode.flealancemarket.service.commands.GetCategoryWithJobs")
    private GetCategoryWithJobs getCategory;



    Object onActivate(int categoryId){
        setCategoryId(categoryId);
        getCategory.setCategoryId(categoryId);
        Category c = getCategory.execute();
        if(c==null) {
            session.setMessage("This category doesn't exist.");
            return Categories.class;
        }
        jobsList = c.getJobs();
        if(jobsList.isEmpty()) {
            session.setMessage("This category doesn't have any job created.");
            return Categories.class;
        }
        return null;
    }
}
